<?php

namespace App\Extensions\Column;

use Encore\Admin\Admin;
use Encore\Admin\Grid\Column\Filter;
use Encore\Admin\Grid\Model;

class RadioFilter extends Filter
{
    /**
     * @var array
     */
    protected $options;
    protected $relation;
    /**
     * @var
     */
    protected $column;


    /**
     * CheckFilter constructor.
     *
     * @param array $options
     * @param string $relation
     * @param string $column
     */
    public function __construct(array $options, $relation = '', $column = '')
    {
        $this->options = $options;

        $this->class = [
            'item' => uniqid('column-filter-item-'),
        ];

        $this->relation = $relation;
        $this->column = $column;
    }

    /**
     * 修正Grid 关联无法获取request值 如；
     * @return array|mixed|string|string[]
     */
    public function getColumnName()
    {
        return str_replace('.', '_', $this->parent->getName());
    }

    /**
     * Add a binding to the query.
     *
     * @param array $value
     * @param Model $model
     */
    public function addBinding($value, Model $model)
    {
        $value = request($this->getColumnName());
        if ($value === '' || is_null($value)) {
            return;
        }

        if ($this->relation instanceof \Closure) {
            $callback = $this->relation->bindTo($model);
            call_user_func_array($callback, [$model, $value]);
        } elseif ($this->relation) {
            $model->whereHas($this->relation, function ($query) use ($value) {
                return $query->where($this->column, $value);
            });
        } else {
            $model->where($this->getColumnName(), $value);
        }
    }

    /**
     * Add script to page.
     *
     * @return void
     */
    protected function addScript()
    {
        $script = <<<SCRIPT
$('.{$this->class['item']}').iCheck({
    radioClass:'iradio_minimal-blue'
});
SCRIPT;

        Admin::script($script);
    }

    /**
     * Render this filter.
     *
     * @return string
     */
    public function render()
    {
        $value = $this->getFilterValue();

        $lists = collect($this->options)->map(function ($label, $key) use ($value) {
            if ($value === '' || is_null($value)) {
                $checked = '';
            } else {
                $checked = $key == $value ? 'checked' : '';
            }
            return <<<HTML
<li class="checkbox icheck" style="margin: 0;">
    <label style="width: 100%;padding: 3px;">
        <input type="radio" class="{$this->class['item']}" name="{$this->getColumnName()}" value="{$key}" {$checked}/>&nbsp;&nbsp;&nbsp;{$label}
    </label>
</li>
HTML;
        })->implode("\r\n");

        $this->addScript();
        $active = empty($value) && $value !== '0' ? '' : 'text-yellow';

        return <<<EOT
<span class="dropdown">
<form action="{$this->getFormAction()}" pjax-container style="display: inline-block;">
    <a href="javascript:void(0);" class="dropdown-toggle {$active}" data-toggle="dropdown">
        <i class="fa fa-filter"></i>
    </a>
    <ul class="dropdown-menu" role="menu" style="padding: 10px;box-shadow: 0 2px 3px 0 rgba(0,0,0,.2);left: -70px;border-radius: 0;">

        <li>
            <ul style='padding: 0;'>
                {$lists}
            </ul>
        </li>
        <li class="divider"></li>
        <li class="text-right">
            <button class="btn btn-sm btn-flat btn-primary pull-left" data-loading-text="{$this->trans('search')}..."><i class="fa fa-search"></i>&nbsp;&nbsp;{$this->trans('search')}</button>
            <span><a href="{$this->getFormAction()}" class="btn btn-sm btn-flat btn-default"><i class="fa fa-undo"></i></a></span>
        </li>
    </ul>
</form>
</span>
EOT;
    }
}
